// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ExtensionsCmdlets.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Cmdlet
{
    #region

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;

    #endregion

    /// <summary>
    /// The extensions.
    /// </summary>
    public class ExtensionsCmdlets
    {
        #region Constants and Fields

        /// <summary>
        /// The command groups.
        /// </summary>
        public static readonly IDictionary<string, CmdletGroup> CmdletGroups;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes static members of the <see cref="ExtensionsCmdlets"/> class.
        /// </summary>
        static ExtensionsCmdlets()
        {
            CmdletGroups = new Dictionary<string, CmdletGroup>();

            // Any file containing the string "extension", case insensitive is loaded for introspection
            var files = from f in Directory.GetFiles(".")
                        where f.IndexOf("extension", StringComparison.InvariantCultureIgnoreCase) >= 0
                        select f;

            // Load extension files and introspect
            foreach (var file in files)
            {
                // Load current extension file
                var aname = Path.GetFileName(file);
                aname = aname.Replace(Path.GetExtension(file), string.Empty);

                // Try to load assembly
                Assembly a = null;
                Devel.NoThrow(() => { a = Assembly.Load(aname); });
                if (a != null)
                {
                    // Look for types which derive of CmdletGroup
                    var types = from t in a.GetTypes()
                                where Reflection.InheritsOf(t, typeof(CmdletGroup).FullName)
                                select t;

                    // Create an instance of each found CmdletGroup and store it into dictionary
                    foreach (var type in types)
                    {
                        CmdletGroup cmdletGroup = null;
                        Devel.NoThrow(() => cmdletGroup = Activator.CreateInstance(type) as CmdletGroup);
                        if (cmdletGroup != null)
                        {
                            CmdletGroups[cmdletGroup.Name] = cmdletGroup;
                        }
                    }
                }
            }
        }

        #endregion
    }
}